
from nicegui import ui
import shelve
from config import database_path,appreciation_code
import time
import hashlib
import  uuid 

# esn=uuid.getnode()


authentication={'esn':uuid.getnode(),'end_time':2111,'status':True}

lic='esn'+'到期时间'+'md5'

def active_device_license(license_input:str):
    license_input_list=license_input.split('-')
    esn_str=license_input_list[0]
    end_time_str=license_input_list[1]
    md5_str=license_input_list[2]
    hash_object = hashlib.md5(f'{esn_str}{end_time_str}duanfu'.encode('utf-8'))
    hash_value = hash_object.hexdigest()
    if md5_str==hash_value and  uuid.getnode() == int(esn_str):
        with shelve.open(database_path) as db:
            # db['authentication']={'esn':uuid.getnode() ,'end_time':2023,'status':False,'试用资格':True}
            db['authentication']={'esn':uuid.getnode() ,'end_time':int(end_time_str),'status':False,'试用资格':True}
            
        splitter_before.refresh()
    else:
        with ui.dialog(value=True) as dialog, ui.card():
                ui.label('认证失败！！')
                ui.label(f'发送ESN:{uuid.getnode()}码到duanfu456@163.com 获取永久序列号')
                ui.button('关闭', on_click=dialog.close)
        
    

def probationary_30_days():
    with shelve.open(database_path) as db:
        authentication=db['authentication']
        # 判断是否试用资格
    if authentication['试用资格']:
        authentication={'esn':uuid.getnode() ,'end_time':int(time.time())+60*60*24*30,'status':False,'试用资格':False}
        with shelve.open(database_path) as db:
            db['authentication']=authentication
            
        splitter_before.refresh()
    else:
            # print('试用资格已过期')
        with ui.dialog(value=True) as dialog, ui.card():
            ui.label('试用资格已经过期！！！')
            ui.label(f'发送ESN:{uuid.getnode()}码到duanfu456@163.com 获取永久序列号')
            ui.button('关闭', on_click=dialog.close)
        
    
    
@ui.refreshable
def splitter_before():
    device_esn=uuid.getnode()
    with shelve.open(database_path) as db:
        authentication=db['authentication']
        if device_esn !=authentication['esn']:
            authentication={'esn':device_esn ,'end_time':2023,'status':False,'试用资格':True}
            # db['authentication']=authentication
        
    end_time=authentication['end_time']
    if time.time() > end_time:
        with ui.row():
            ui.label(f'设备ESN码 :')
            ui.input(value=f'{authentication['esn']}').classes('w-96')
        with ui.row():
            ui.label(f'设备序列号:')
            license_input=ui.input(label=f'输入正确的激活序列号',placeholder='输入正确的激活序列号').classes('w-96')
        with ui.row():  
            button=ui.button('激活程序').on('click',lambda:active_device_license(license_input.value))
            button=ui.button('试用30天').on('click',lambda:probationary_30_days())
        # 设备按钮居中
    else:
        with ui.row():
            ui.label(f'设备ESN码 :')
            ui.input(value=f'{authentication['esn']}').classes('w-96')
            
        ui.label('认证成功！！')
        ui.label(f'在过:{int((end_time-int(time.time()))/(60*60*24))}天后licsens 到期')
    
    ui.label('本工具永久免费使用！！！').classes('text-xl ')
    ui.label(f'发送设备ESN码到: duanfu456@163.com 即可获得序列号！！！').classes('text-xl ')
    ui.label(f'有问题可以联系作者更新').classes('text-xl font-semibold')
    ui.label(f'软件更新邮件通知哦！！！').classes('text-xl font-semibold')
        
    


def splitter_after():
   
    ui.image(appreciation_code).classes('h-64 w-64')
    ui.label('感谢您的使用本工具！！').classes('text-xl ')
    

def page():
    with ui.row().classes('  '): #
        with ui.card().classes('h-96 '):
            splitter_before()
        
        with ui.card().classes('h-96'):
            splitter_after()
 